package com.example.hidesms.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.example.hidesms.R;
import com.example.hidesms.dto.SMSItem;
import com.example.hidesms.utils.Utils;

public class CategoryAdapter extends ArrayAdapter<SMSItem>{
	
	private Context mContext;
	private ArrayList<SMSItem> list;
	private LayoutInflater mInflater;
	private int width;
	private RelativeLayout.LayoutParams inboxParam, sentParam;
	
	public void setList(ArrayList<SMSItem> list) {
		this.list = list;
	}

	public CategoryAdapter(Context context, int textViewResourceId, ArrayList<SMSItem> list) {
		super(context, textViewResourceId, list);
		mContext= context;
		this.list = list;
		this.mInflater = LayoutInflater.from(context);
		width = Utils.getScreenWidth(mContext);
		inboxParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		inboxParam.leftMargin= (int) (width*0.1);
		sentParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		sentParam.rightMargin= (int) (width*0.1);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.category_adapter_item, null);
			holder = new ViewHolder(convertView);
			inboxParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			sentParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		final SMSItem si = list.get(position);
		holder.tvTime.setText(si.getTimeFormated());
		holder.tvContent.setText(si.getMsg());
		if ("inbox".equals(si.getFolderName())) {
			holder.rlParent.setBackgroundResource(R.drawable.chat_come);
			holder.rlParent.setLayoutParams(inboxParam);
			holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.black));
			holder.tvTime.setTextColor(mContext.getResources().getColor(R.color.black));
		}else{
			holder.rlParent.setBackgroundResource(R.drawable.chat_going);
			holder.rlParent.setLayoutParams(sentParam);
			holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.white));
			holder.tvTime.setTextColor(mContext.getResources().getColor(R.color.white));
		}
		
		return convertView;
	}
	
	
	private class ViewHolder{
		private TextView  tvTime, tvContent;
		private RelativeLayout rlParent;
		public ViewHolder(View parent){
			rlParent = (RelativeLayout) parent.findViewById(R.id.rlParent);
			tvTime = (TextView) parent.findViewById(R.id.tvTime);
			tvContent= (TextView) parent.findViewById(R.id.tvContent);
		}
	}

}
